package com.situ.springboot.controller;

import com.situ.springboot.pojo.Dept;
import com.situ.springboot.pojo.query.DeptQuery;
import com.situ.springboot.service.IDeptService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @ResponseBody
    @RequestMapping("/selectAll")
    public Result selectAll() {
        List<Dept> list = deptService.selectAll();
        return Result.ok(list);
    }

    @ResponseBody
    @RequestMapping("/add")
    public Result add(Dept dept) {
        deptService.add(dept);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toDeptAdd")
    public String toDeptAdd() {
        return "dept_add";
    }

    @ResponseBody
    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        deptService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    // /dept/deleteById?id=23
    @ResponseBody
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        deptService.deleteById(id);
        return Result.ok("删除成功");
    }

    // 访问Controller，转发到dept_list.html
    // /dept/toDeptList
    @RequestMapping("/toDeptList")
    public String toDeptList() {
        return "dept_list";
    }

    // /dept/list?page=1&limit=10&name=zhang&email=abc
    @ResponseBody
    @RequestMapping("/list")
    public PageResult list(DeptQuery deptQuery) {
        PageResult pageResult = deptService.list(deptQuery);
        return pageResult;
    }

}
